package com.xinzhi.savingpot.rminfo.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.xinzhi.savingpot.rminfo.mapper.IRmInfoMapper;
import com.xinzhi.savingpot.rminfo.pojo.RminfoVo;
import com.xinzhi.savingpot.rminfo.service.IRminfoService;
import com.xinzhi.savingpot.userinfo.entity.UserInfo;
import com.xinzhi.savingpot.util.BaseResponse;
import com.xinzhi.savingpot.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RminfoImpl implements IRminfoService {
    @Autowired
    IRmInfoMapper iRmInfoMapper;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public BaseResponse findRminfoAll(int userid) {
        RminfoVo rv = new RminfoVo();
        if (!redisUtil.hasKey("rminfo-"+userid)){
            List<UserInfo> rminfoAll = iRmInfoMapper.getRminfoAll(userid);
            if (!rminfoAll.isEmpty()){
                for (UserInfo rm:rminfoAll) {
                    rv.setUserid(rm.getUserNumber());
                    rv.setBalance(rm.getBalance());
                    rv.setEarnestmoney(rm.getEarnestMoney());
                }
                redisUtil.set("rminfo-"+userid,rv,6000);
                return new BaseResponse(200, "查询成功", rv);
            }else {
                return new BaseResponse(400, "查询失败", null);
            }
        }
        return new BaseResponse(200, "查询成功", redisUtil.hasKey("rminfo-"+userid));
    }
}
